package com.tdkj.service.shop.impl;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.tdkj.dao.shop.IBeanGoodAuctionDao;
import com.tdkj.model.shop.BeanGood;
import com.tdkj.model.shop.BeanGoodAuction;
import com.tdkj.model.shop.BeanGoodSnapshot;
import com.tdkj.model.shop.BeanOrder;
import com.tdkj.model.shop.BeanOrderItem;
import com.tdkj.model.shop.BeanShop;
import com.tdkj.service.BaseService;
import com.tdkj.service.shop.IBeanGoodAuctionService;
import com.tdkj.service.shop.IBeanOrderService;
@Component
public class BeanGoodAuctionServiceImpl extends BaseService implements
		IBeanGoodAuctionService {
	
	private  IBeanGoodAuctionDao beanGoodAuctionDao;
    
	@Resource
	public void setBeanGoodAuctionDao(IBeanGoodAuctionDao beanGoodAuctionDao) {
		this.beanGoodAuctionDao = beanGoodAuctionDao;
	}

	@Override
	public void finish(Long id) {
		BeanGoodAuction auction = beanGoodAuctionDao.findById(id, BeanGoodAuction.class);
		auction.setState("1");
		
		BeanGood good = auction.getGood();
			
		BeanGoodAuction newAuction = new BeanGoodAuction();
		newAuction.setStartTime(new Date(System.currentTimeMillis()));
		newAuction.setEndTime(new Date(System.currentTimeMillis() + 1000L*24*60*60*good.getEffective()));
		newAuction.setGood(good);
		newAuction.setState("0");
		beanGoodAuctionDao.save(newAuction);
			
		good.setCurrentAuction(newAuction);
		
//		BeanGood good = auction.getGood();
//		BeanShop shop = good.getShop();
//		BeanOrder order = new BeanOrder();
//		order.setCode(UUID.randomUUID().toString());
//		order.setSeller(shop.getUser());
//		order.setBuyer(auction.getFinalQuote().getUser());
//		//order.setConsigneeInfo(info);
//		order.setState(IBeanOrderService.Constant.STATE_TO_PAY);
//		order.setCreateTime(new Timestamp(System.currentTimeMillis()));
//		order.setCutoffPaymentTime(new Timestamp(System.currentTimeMillis() + 7L*24*60*60*1000));
//		beanGoodAuctionDao.save(order);
//				
//			
//		BeanGoodSnapshot snapshot = new BeanGoodSnapshot();
//		snapshot.setCode(good.getCode());
//		snapshot.setDetailDescription(good.getDetailDescription());
//		snapshot.setGood(good);
//		snapshot.setImgurl(good.getImgurl());
//		snapshot.setKeywords(good.getKeywords());
//			
//		snapshot.setLogisticsType(good.getLogisticsInfo().getType());
//		if("0".equals(good.getLogisticsInfo().getType())){
//			snapshot.setLogisticsPrice(good.getLogisticsInfo().getEmsPrice());
//		}
//		snapshot.setName(good.getName());
//		snapshot.setPrice(good.getPrice());
//		
//		Map<String,String> properties = new HashMap<String,String>();
//		for(String key : good.getProperties().keySet()){
//			properties.put(key, good.getProperties().get(key));
//		}
//		snapshot.setProperties(properties);
//		snapshot.setPublishTime(good.getPublishTime());
//		snapshot.setQuantity(good.getQuantity());
//		snapshot.setRateOfIncrease(good.getRateOfIncrease());
//		snapshot.setSellType(good.getSellType());
//		snapshot.setState(good.getState());
//		snapshot.setTypeName(good.getType().getName());
//		snapshot.setUser(good.getUser());
//		snapshot.setUseType(good.getUseType());
//			
//		beanGoodAuctionDao.save(snapshot);
//
//		BeanOrderItem orderItem = new BeanOrderItem();
//		orderItem.setQuantity(auction.getQuantity());
//		orderItem.setSnapshot(snapshot);
//		orderItem.setGood(good);
//		orderItem.setOrder(order);
//		orderItem.setState(0);
//		beanGoodAuctionDao.save(orderItem);
//			
//		order.getItems().add(orderItem);
//		
//		//计费
//		BigDecimal totalFee = new BigDecimal("0.00");
//		for(BeanOrderItem item : order.getItems()){
//			totalFee = totalFee.add(item.getQuantity().multiply(item.getSnapshot().getPrice()));
//		}
//		order.setTotalFee(totalFee);
//		
//		auction.setGood(good);
	}

}
